<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hAdd"
                >新增角色</el-button
              >
            </el-row>
            <!-- 表格 -->
            <el-table :data="roles">
              <el-table-column label="序号" type="index" width="120" />
              <el-table-column label="角色名称" prop="name" width="240" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button size="small" @click="hAssign(scope.row.id)"  type="success">分配权限</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click="hEdit(scope.row)"
                    >编辑</el-button
                  >
                  <el-button
                    size="small"
                    type="danger"
                    @click="hDel(scope.row.id)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>

            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                @current-change="hCurrentChange"
                @size-change="hSizeChange"
                :total="total"
                :current-page="page"
                :page-size="pagesize"
                :page-sizes="[1, 2, 3, 4]"
                layout="sizes,prev,pager,next"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 新增弹框 -->
    <el-dialog
      @close="hClose"
      :title="isEdit ? '编辑' : '添加'"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="showDialog"
    >
      <el-form
        ref="roleForm"
        :model="roleForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="showDialog = false">取消</el-button>
          <el-button size="small" type="primary" @click="hSubmit"
            >确定</el-button
          >
        </el-col>
      </el-row>
    </el-dialog>
    <!-- 分配权限的弹层 -->
    <el-dialog
      v-if="showDialogAssign"
      title="分配权限(一级为路由页面查看权限-二级为按钮操作权限)"
      :visible.sync="showDialogAssign"
    >
      <assign-permission 
      :roleId="roleId"
      @success='hSuccess'
      />
    </el-dialog>
  </div>
</template>
<script>
import { getRoles, deleteRole, addRole, editRole } from "@/api/settings";
import assignPermission from "./assignPermission";
export default {
  components: {
    assignPermission,
  },
  data() {
    return {
      roleId:'',//给子组件传入点击id
      showDialogAssign:false,//修改权限弹框
      pagesize: 2,
      page: 1,
      roles: [],
      total: 0,
      //新增部门弹框
      roleForm: {
        name: "",
        description: "",
      },
      showDialog: false,
      //定义编辑和修改共用一个弹窗
      isEdit: false,
      rules: {
        name: [
          { required: true, message: "角色名称不能为空", trigger: "blur" },
        ],
        description: [
          { required: true, message: "角色名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.loadRoles();
  },
  methods: {
    hSuccess(){
      this.showDialogAssign = false
      // 提示
      this.$message.success('成功')
    }
    //把id传给子组件
    ,
    hAssign(id){
      this.showDialogAssign = true
      this.roleId  = id
    }
    ,
    hClose() {
      this.$refs.roleForm.resetFields();
    },
    //编辑
    hEdit(data) {
      this.isEdit = true;
      console.log(data);
      this.showDialog = true;
      this.roleForm = { ...data };
    },

    async doAdd() {
      try {
        //发送请求
        await addRole(this.roleForm);
        //添加数据后跳转到正确页面
        this.total++;
        this.page = Math.ceil(this.total / this.pagesize);
        //重新获取数据
        this.loadRoles();
        //提示消息
        this.$message.success("添加成功");
        //关闭弹窗
        this.showDialog = false;
      } catch (error) {
        this.$message.error("添加失败了");
      }
    },
    async doEdit() {
      try {
        //发送请求
        await editRole(this.roleForm);
        //重新获取数据
        this.loadRoles();
        //提示消息
        this.$message.success("修改成功");
        //关闭弹窗
        this.showDialog = false;
      } catch (error) {
        this.$message.error("添加失败了");
      }
    },
    //新增
    hSubmit() {
      this.$refs.roleForm.validate((valida) => {
        if (valida) {
          this.isEdit ? this.doEdit() : this.doAdd();
        }
      });
    },
    hAdd() {
      this.showDialog = true;
      this.isEdit = false;
    },
    //删除
    async doDel(id) {
      try {
        await deleteRole(id);
        this.$message.success("删除成功");
        if (this.roles.length === 1 && this.page > 1) {
          this.page--;
        }
        this.loadRoles();
      } catch (err) {
        this.$message.error("删除失败");
      }
    },
    hDel(id) {
      this.$confirm("此操作将永久删除该角色, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.doDel(id);
        })
        .catch((err) => {
          console.log(err);
        });
    },
    hSizeChange(curPagesize) {
      this.pagesize = curPagesize;
      this.page = 1;
      this.loadRoles();
    },
    hCurrentChange(curPage) {
      this.page = curPage;
      this.loadRoles();
    },
    async loadRoles() {
      try {
        const res = await getRoles({
          page: this.page,
          pagesize: this.pagesize,
        });
        console.log(res);
        this.roles = res.data.rows;
        this.total = res.data.total;
      } catch (error) {
        console.log(err);
      }
    },
  },
};
</script>
